<?php

namespace App\Http\Controllers\App\Vote;

use Illuminate\Http\Request;
use App\App\Services\VoteService;
use App\Http\Controllers\Controller;

class DetailsController extends Controller
{
    protected $vote;

    public function __construct(VoteService $vote)
    {
        $this->vote = $vote;
    }

    public function show(Request $request, $id)
    {
        if (!$this->vote->bootstraped($id)) {
            $this->vote->bootstrap($id);
        }

        $uid = $request->input('uid', null);

        if ($uid) {
            $this->vote->registerUid($uid);
        }
        $queryString = $this->getQueryString($request->input());
        $backgroundImg = $this->getBackgroundImg($id);

        return view('app.vote.unregister.list', [
            'voteId'        => $id,
            'uid'           => $uid,
            'signed'        => $request->request->get('signed'),
            'queryString'   => implode('&', $queryString),
            'backgroundImg' => $backgroundImg,
        ]);
    }

    public function search(Request $request, $id)
    {
        $backgroundImg = '';
        if ($this->vote->bootstraped($id)) {
            $backgroundImg = $this->getBackgroundImg($id);
        }
        $queryString = $this->getQueryString($request->input());

        return view('app.vote.unregister.listSearch', [
            'signed'        => $request->request->get('signed'),
            'vote_id'       => $id,
            'voteId'        => $id,
            'uid'           => $request->input('uid'),
            'queryString'   => implode('&', $queryString),
            'backgroundImg' => $backgroundImg,
        ]);
    }

    public function sortlist(Request $request, $id)
    {
        $backgroundImg = '';
        if ($this->vote->bootstraped($id)) {
            $backgroundImg = $this->getBackgroundImg($id);
        }
        $queryString = $this->getQueryString($request->input());

        return view('app.vote.unregister.listSort', [
            'signed' => $request->request->get('signed'),
            'vote_id' => $id,
            'voteId' => $id,
            'uid' => $request->input('uid'),
            'queryString' => implode('&', $queryString),
            'backgroundImg' => $backgroundImg,
        ]);
    }

    public function rule(Request $request, $id)
    {
        $backgroundImg = '';
        if ($this->vote->bootstraped($id)) {
            $backgroundImg = $this->getBackgroundImg($id);
        }
        $queryString = $this->getQueryString($request->input());

        return view('app.vote.unregister.rules', [
            'signed' => $request->request->get('signed'),
            'vote_id' => $id,
            'voteId' => $id,
            'uid' => $request->input('uid'),
            'queryString' => implode('&', $queryString),
            'backgroundImg' => $backgroundImg,
        ]);
    }

    private function getBackgroundImg($id)
    {
        $backgroundImg = '';
        $voteCache = $this->vote->getVoteCache($id);
        if ($voteCache) {
            $voteCache = \GuzzleHttp\json_decode($voteCache, 1);
            $backgroundImg = $voteCache['background_img'];
        }

        return $backgroundImg;
    }

    private function getQueryString($params)
    {
        $queryString = [];
        if ($params) {
            foreach ($params as $key => $item) {
                if (in_array($key, ['url', 'signed'])) {
                    continue;
                }
                $queryString[] = $key . '=' . $item;
            }
        }

        return $queryString;
    }
}
